import requests
import time
from bs4 import BeautifulSoup
import os

def download(url):
    headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0"
    }
    try:
        response = requests.get(url, headers=headers, verify=False)
        response.raise_for_status()
        response.encoding = response.apparent_encoding
        time.sleep(1)
        return response.content
    except:
        print(url, "访问错误")
        return ""

file = "../readexcel/sanchang"
if not os.path.isdir(file):
    os.mkdir(file)
url = "https://www.sanchuangedu.cn/"
html = download(url)
soup = BeautifulSoup(html, "lxml")
pic_all = soup.find_all("img")
for pic in pic_all:
    allsrc = pic.attrs.get("src")
    src = f"https://www.sanchuangedu.cn/{allsrc}"
    filename = f"{file}/{allsrc}"
    with open(filename, "wb") as f:
        f.write(download(src))

